// vim:ft=javascript
// $Id$
// "Master" config file; think of it as a configure.in equivalent.

// MSVC version table
var MSVCVERS = [];
MSVCVERS[1200] = 'MSVC6 (Visual C++ 6.0)';
MSVCVERS[1300] = 'MSVC7 (Visual C++ 2002)';
MSVCVERS[1310] = 'MSVC7.1 (Visual C++ 2003)';
MSVCVERS[1400] = 'MSVC8 (Visual C++ 2005)';
MSVCVERS[1500] = 'MSVC9 (Visual C++ 2008)';
MSVCVERS[1600] = 'MSVC10 (Visual C++ 2010)';
MSVCVERS[1700] = 'MSVC11 (Visual C++ 2012)';

STDOUT.WriteBlankLines(1);

CL = PATH_PROG('cl');

// avoid picking up midnight commander from cygwin
PATH_PROG('mc', WshShell.Environment("Process").Item("PATH"));
PATH_PROG('rc', WshShell.Environment("Process").Item("PATH"));

/* cygwin ships with 'link.exe'. Avoid searching the cygwin path for this, as we want the MS linker */
PATH_PROG('link', WshShell.Environment("Process").Item("PATH"), "LD");
PATH_PROG('nmake');

// check for grep and sed!
if (!PATH_PROG('grep', WshShell.Environment("Process").Item("PATH")) ||
	!PATH_PROG('sed', WshShell.Environment("Process").Item("PATH"))) {
 	ERROR("grep and sed are required");
} 

// we don't want to define LIB, as that will override the default library path set in that env var
PATH_PROG('lib', null, 'MAKE_LIB');
PATH_PROG('php');

if (!PATH_PROG('php', WshShell.Environment("Process").Item("PATH"))) {
 	ERROR("php is required");
} 

STDOUT.WriteBlankLines(1);

if (CL) {
	var VCVERS = probe_msvc_compiler_version(CL);
	STDOUT.WriteLine("Detected compiler version: " + VCVERS);

	if (typeof(MSVCVERS[VCVERS]) == undefined) {
		// Lets just bail out if we dont know the MSVC version
		ERROR("MS C++ compiler is required -- unable to detect version");
	} else {
		STDOUT.WriteLine("Detected compiler version: " + MSVCVERS[VCVERS]);
 	}
} else {
	ERROR("MS C++ compiler is required");
}

STDOUT.WriteBlankLines(1);

ARG_WITH('php-build', 'Path to where you extracted http://www.php.net/extra/win32build.zip. Assumes that it is a sibling of this source dir (..\\php_build) if not specified', 'no');
ARG_WITH('cygwin', 'Path to cygwin utilities on your system', '\\cygwin');
ARG_ENABLE('gd', 'Php has static gd', 'no');
ARG_ENABLE('zts', 'Disable thread safety', 'yes');

// stick objects somewhere outside of the source tree
ARG_ENABLE('object-out-dir', 'Alternative location for binary objects during build', '');
if (PHP_GTK_OBJECT_OUT_DIR.length) {
	if (!FSO.FolderExists(PHP_GTK_OBJECT_OUT_DIR)) {
		ERROR('Your chosen output directory ' + PHP_GTK_OBJECT_OUT_DIR + ' does not exist');
	}
	PHP_GTK_OBJECT_OUT_DIR += '\\';
}

ARG_ENABLE('debug', 'Compile with debugging symbols', "no");
ARG_ENABLE('debug-pack', 'Release binaries with external debug symbols (--enable-debug must not be specified)', 'no');
if (PHP_GTK_DEBUG == "yes" && PHP_GTK_DEBUG_PACK == "yes") {
	ERROR("Use of both --enable-debug and --enable-debug-pack not allowed.");
}

// Compile directory
if (PHP_GTK_DEBUG == "yes" && PHP_GTK_ZTS == "yes") {
	DEFINE("BUILD_DIR", PHP_GTK_OBJECT_OUT_DIR + "Debug_TS");
	DEFINE("PHP_LIB", "php5ts_debug.lib");
	PHP_LIB = "php5ts_debug.lib";
} else if (PHP_GTK_DEBUG == "yes") {
	DEFINE("BUILD_DIR", PHP_GTK_OBJECT_OUT_DIR + "Debug");
	DEFINE("PHP_LIB", "php5_debug.lib");
	PHP_LIB = "php5_debug.lib";
} else if (PHP_GTK_ZTS == "yes") {
	DEFINE("BUILD_DIR", PHP_GTK_OBJECT_OUT_DIR + "Release_TS");
	DEFINE("PHP_LIB", "php5ts.lib");
	PHP_LIB = "php5ts.lib";
} else {
	DEFINE("BUILD_DIR", PHP_GTK_OBJECT_OUT_DIR + "Release");
	DEFINE("PHP_LIB", "php5.lib");
	PHP_LIB = "php5.lib";
}
if (PHP_GTK_PHP_BUILD == 'no') {
	if (FSO.FolderExists("..\\php_gtk_build")) {
		PHP_GTK_PHP_BUILD = "..\\php_gtk_build";
	} else if (FSO.FolderExists("..\\php_build")) {
		PHP_GTK_PHP_BUILD = "..\\php_build";
	} else if (FSO.FolderExists("..\\win32build")) {
		PHP_GTK_PHP_BUILD = "..\\win32build";
	} else if (FSO.FolderExists("..\\php-win32-dev\\php_build")) {
		PHP_GTK_PHP_BUILD = "..\\php-win32-dev\\php_build";
	}
}

var php_usual_include_suspects = PHP_GTK_PHP_BUILD;
var php_usual_lib_suspects = PHP_GTK_PHP_BUILD;

ARG_IS('all', 'Configure all available extensions', 'no');

DEFINE("PHPGTKDLL", "php_gtk2.dll");
DEFINE("PHPGTKLIB", "php_gtk2.lib");
get_version_numbers();
/* pick up the version from gtk/gtkversion.h */
get_gtk_libversion();

// General link flags
DEFINE("LDFLAGS", "/nologo");
// General DLL link flags
DEFINE("DLL_LDFLAGS", "/dll /machine:I386");
// PHP-GTK DLL link flags
DEFINE("LDFLAGS_PHP_GTK", "$(DLL_LDFLAGS)");

// General libs
DEFINE("LIBS", "kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib");

if (PHP_GTK_DEBUG == "yes") {
	DEFINE("CFLAGS", "/nologo /LDd /MDd /Gm /ZI /GZ /I . /I ..\\ /I ..\\.. /I main /I ..\\main /I ext /I ..\\ext /D ZEND_WIN32 /D ZEND_DEBUG=1 /D PHP_WIN32 /D WIN32 /D _DEBUG /D _WINDOWS /D _MBCS /D _USRDLL");
	ADD_FLAG("LDFLAGS", "/debug");

} else {
	DEFINE("CFLAGS", "/nologo /MD /W3 /EHsc /Ox /I . /I ..\\ /I ..\\.. /I main /I ..\\main /I ext /I ..\\ext /D ZEND_WIN32 /D ZEND_DEBUG=0 /D PHP_WIN32 /D WIN32 /D NDEBUG /D _WINDOWS /D _MBCS /D _USRDLL");
	// Generate external debug files when --enable-debug-pack is specified
	if (PHP_GTK_DEBUG_PACK == "yes") {
	 	ADD_FLAG("CFLAGS", "/Zi");
	 	ADD_FLAG("LDFLAGS", "/incremental:no /debug /opt:ref,icf");
	 }
}

if (PHP_GTK_ZTS == "yes") {
	 	ADD_FLAG("CFLAGS", "/D ZTS=1");
	 }

if (VCVERS < 1400) {
	// Enable automatic precompiled headers
	ADD_FLAG('CFLAGS', '/YX /c');
} else {
	// fun stuff: MS deprecated ANSI stdio and similar functions - disable warnings
	// GS hardens the binary against buffer overruns
	ADD_FLAG('CFLAGS', '/GS /wd4996 ');
}

if (PHP_GTK_GD == "yes") {
	ADD_FLAG('CFLAGS', '/D HAVE_GD_BUNDLED=1'); 
}

probe_basic_headers();

ADD_SOURCES("main", "php_gtk.c php_gtk_ext.w32.c php_gtk_util.c phpg_closure.c phpg_exceptions.c phpg_gboxed.c phpg_gobject.c phpg_gpointer.c phpg_gtype.c phpg_gvalue.c phpg_atom.c phpg_support.c phpg_paramspec.c", "php-gtk");

